#pragma once

//调用门传参
#define  SYSCALL_PARAM_COUNT   5

typedef struct _syscall_frame_t 
{
	int eflags;
	int gs, fs, es, ds;
	int edi, esi, ebp, dummy, ebx, edx, ecx, eax;
	int eip, cs;
	int func_id, arg0, arg1, arg2, arg3;
	int esp, ss;
}syscall_frame_t;

void exception_handler_syscall();

// 系统调用包装宏
// 注意：所有参数通过 uint32_t 传递，需强制转换

// 无参数系统调用
#define SYSCALL_WRAP0(name) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
return sys_##name(); \
}

// 1个参数，返回 int
#define SYSCALL_WRAP1(name, t1) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
return sys_##name((t1)a0); \
}

// 2个参数，返回 int
#define SYSCALL_WRAP2(name, t1, t2) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
return sys_##name((t1)a0, (t2)a1); \
}

// 3个参数，返回 int
#define SYSCALL_WRAP3(name, t1, t2, t3) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
return sys_##name((t1)a0, (t2)a1, (t3)a2); \
}

// 4个参数，返回 int
#define SYSCALL_WRAP4(name, t1, t2, t3, t4) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
return sys_##name((t1)a0, (t2)a1, (t3)a2, (t4)a3); \
}

// 1个参数，无返回值 (void)
#define SYSCALL_WRAP_VOID1(name, t1) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
sys_##name((t1)a0); \
return 0; \
}

// 2个参数，无返回值 (void)
#define SYSCALL_WRAP_VOID2(name, t1, t2) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
sys_##name((t1)a0, (t2)a1); \
return 0; \
}

// 特殊：sbrk 返回 char*，但系统调用表要求返回 int
#define SYSCALL_WRAP_SBRK(name, t1) \
int sys_##name##_wrapper(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3) { \
char* ret = sys_##name((t1)a0); \
return (int)ret; \
}